package com.artifact.mit.mypoint.service.async;

import static com.artifact.mit.mypoint.service.util.FormatHelper.appendImageUrl;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.artifact.mit.mypoint.activity.MyPointBaseActivity;
import com.artifact.mit.mypoint.adapter.NewsAdapter;
import com.artifact.mit.mypoint.service.ServiceHandler;
import com.artifact.mit.mypoint.service.model.SetImage;
import com.artifact.mit.mypoint.service.model.api.News;
import com.artifact.mit.mypoint.service.model.api.News.NewsDetail;

public class NewsAsync extends AsyncTask<String, Void, News>{

	private TextView newsText1;
	private TextView newsText2;
	private TextView newsText3;
	private TextView newsText4;
	private ImageView newsImage1;
	private ImageView newsImage2;
	private ListView newsList;
	
	private boolean isForDasboard;
	private Context newsContext;
	private ServiceHandler serviceHandler;
	
	public NewsAsync(Context context, TextView newsName1, TextView newsDesc1, ImageView newsImg1, 
			TextView newsName2, TextView newsDesc2, ImageView newsImg2, boolean forDasboard) {
		serviceHandler = new ServiceHandler(context);
		newsText1 = newsName1;
		newsText2 = newsDesc1;
		newsImage1 = newsImg1;
		newsText3 = newsName2;
		newsText4 = newsDesc2;
		newsImage2 = newsImg2;
		isForDasboard = forDasboard;
	}
	
	public NewsAsync(Context context, ListView list, boolean forDasboard) {
		newsContext = context;
		serviceHandler = new ServiceHandler(context);
		newsList = list;
		isForDasboard = forDasboard;
	}
	
	private void setProgress(int visibility){
		MyPointBaseActivity activity = ((MyPointBaseActivity) newsContext);
		if (activity != null)
			activity.setProgressBar(visibility);
	}
	
	@Override
	protected void onPreExecute() {
		setProgress(View.VISIBLE);
	}
	
	@Override
	protected void onPostExecute(News result) {
		if (isForDasboard && result.getNewsDetail() != null && result.getNewsDetail().length >= 2) {
			for (int i=0; i<result.getNewsDetail().length;) {
				newsText1.setText(result.getNewsDetail()[i].getTitle());
				newsText2.setText(result.getNewsDetail()[i].getHeadLine());
				new ImageLoader().execute(new SetImage(newsImage1, appendImageUrl(result.getNewsDetail()[i].getUrlImage()), false));
				
				newsText3.setText(result.getNewsDetail()[i+1].getTitle());
				newsText4.setText(result.getNewsDetail()[i+1].getHeadLine());
				new ImageLoader().execute(new SetImage(newsImage2, appendImageUrl(result.getNewsDetail()[i+1].getUrlImage()), false));	
				setProgress(View.GONE);
				break;
			}			
		} else {
			NewsAdapter newsAdapter = new NewsAdapter(newsContext);
			for (NewsDetail news : result.getNewsDetail()) {
				newsAdapter.add(news);
			}
			newsList.setAdapter(newsAdapter);
			setProgress(View.GONE);
		}
	}
	
	@Override
	protected News doInBackground(String... params) {
		try {
			return serviceHandler.newsService();
		} catch (Exception e) {
			Log.e(this.getClass().getName(), "Error News Service", e);
		}
		return null;
	}

}
